﻿/* $RCSFile: UsersDownLoads.ascx.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/AddonNice.ECommerce/PageModules/Download/UsersDownLoads.ascx.cs $
 * $log$
 * Revision 21 2010/08/09 11:08:11  christian.surieux
 *   Added Log comment to all .ascx .aspx .master files
 * Revision 8 2010/08/09 10:07:05  christian.surieux
 *   Added Log comment to all cs files
 */

using System;
using System.Globalization;
using System.Diagnostics;
using System.IO;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Collections.Generic;
using System.Web.Security;
using System.Xml;
using System.Configuration;

using AddonNice;
using AddonNice.Authentication;
using AddonNice.UI;
using AddonNice.UI.WebControls;
using AddonNice.Configuration;
using AddonNice.Configuration.Settings;
using AddonNice.Security;
using AddonNice.UI.DataTypes;
using AddonNice.Helpers;
using AddonNice.Diagnostics;
using AddonNice.Settings;
using AddonNice.Core;

using AddonNice.CRMWrapper;
using AddonNice.CRMWrapper.Design;
using AddonNice.CRMWrapper.ECommerce;

using AddonNice.CRMImage;
using AddonNice.CRMImage.UI;

using AddonNice.ECommerce;
using AddonNice.ECommerce.Helpers;
using AddonNice.ECommerce.Design;
using AddonNice.ECommerce.UI;
using AddonNice.ECommerce.UI.WebControls;
using AddonNice.Settings.Download;
using AddonNice.Settings.ECommerce;

using Telerik.Web.UI;

using Trace2    =   System.Diagnostics.Trace;


namespace AddonNice.PageModules.Download
{
    /// <summary>
    /// Initializer class for UsersDownLoads 
    /// </summary>ECommerceModule
    public class UsersDownLoadsSI : ECommerceModuleSI
    {
        public UsersDownLoadsSI(ModuleSettingsDictionary bSettings,ISettingsOwner pm) : base()
        {
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]UsersDownLoadsSI Ctor BEG.",ModuleTraceSwitch.Sw.Info);
            InitSettings(bSettings,pm);
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]UsersDownLoadsSI Ctor END.",ModuleTraceSwitch.Sw.Info);
        }
 
        public override void InitSettings(ModuleSettingsDictionary bSettings,ISettingsOwner pm)
        {             
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]UsersDownLoadsSI InitSettings BEG.",ModuleTraceSwitch.Sw.Info);
            
            base.InitSettings(bSettings,pm);

            string extgrp                       =   pm.ExtendedResourcesGroup;

            NumericDataType wdt                 =   new NumericDataType();
            wdt.DecimalDigits                   =   0;
			SettingItemSpecial WarrantyPeriod   =	new SettingItemSpecial(EComStStr.WarrantyPeriod,wdt);
			WarrantyPeriod.Required		        =	true;
            WarrantyPeriod.CouldOverride        =   false;
			WarrantyPeriod.Value		        =	"6";
			WarrantyPeriod.Order		        =	1;
            WarrantyPeriod.Group                =   SettingItemGroup.MODULE_SPECIAL_SETTINGS; // necessary to split tabs
            WarrantyPeriod.ExtendedGroupName    =   extgrp;
            WarrantyPeriod.ResourceFile         =   EComStStr.ResourceFile;
            WarrantyPeriod.ResourceItem         =   EComStStr.ResourceItem;
			WarrantyPeriod.EnglishName	        =	"Warranty period (months)";
			WarrantyPeriod.Description	        =	"Warranty period in months when buying something first time.";
			bSettings.AddSetting(WarrantyPeriod);

            AddRadGridPack(bSettings,10,extgrp);
        }
    }

    public partial class UsersDownLoads : ECommerceModule
    {
        Prod2DownLoadDB dwnDB   =   new Prod2DownLoadDB();

        /// <summary>
        /// No need to be attached to a cm entity
        /// </summary>
        public override bool NeedAttachedEntity
        {
            get
            {
                return false;
            }
        }

        /// <summary>
        int WarrantyPeriod
        {
            get
            {
                return Settings[EComStStr.WarrantyPeriod].ToInt();
            }
        }

		#region Global Implementation

        public override Guid GuidID 
		{
			get
			{
				return new Guid(EComStStr.UsersDownLoadsGuid);
			}
		}

		#endregion Global Implementation
        
        #region Ctor

        /// <summary>
		/// Ctor Set the properties of the module
		/// </summary>
        public UsersDownLoads()
        {
            if (TraceProducts.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]UsersDownLoads Ctor Called at: {0}", DateTime.UtcNow), TraceProducts.Sw.Info);
        }

        public override string ExtendedResourcesGroup 
		{
            get
            {
       			const string extgrp                 =   "USERSDOWNLOADS";
                return extgrp;
            }
		}

        public override void InitPortalModuleControl(ModuleSettingsDictionary bSettings)
        {
            PortalModuleInitializer =   new UsersDownLoadsSI(bSettings,this);
        }

        #endregion Ctor

        protected void Page_Load(object sender, EventArgs e)
        {
			if (ModuleTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]UsersDownLoads Page_Load BEG IsPostBack: {0}",IsPostBack),ModuleTraceSwitch.Sw.Info);
            if ( ! IsPostBack )
            {
				//force paging according settings
				RadGrid1.AllowPaging	        =	Settings[EComStStr.Paging].ToBool();
                RadGrid1.AllowFilteringByColumn =	Settings[EComStStr.AllowFilteringByColumn].ToBool();
                RadGrid1.GroupingEnabled        =	Settings[EComStStr.GroupingEnabled].ToBool();
				if ( RadGrid1.AllowPaging )
						RadGrid1.PageSize = Settings[EComStStr.PageSz].ToInt();
            }
        }

        protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (ModuleTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]UsersDownLoads RadGrid1_ItemDataBound BEG e.Item.OwnerTableView.Name: {0}, e.Item: {1}",
                    e.Item.OwnerTableView.Name,e.Item),ModuleTraceSwitch.Sw.Info);
            if ( e.Item.OwnerTableView.Name == "Users" )
            {
                if (e.Item is GridEditFormItem )
                {
                    if (ModuleTraceSwitch.Sw.TraceVerbose)
                        Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]UsersDownLoads RadGrid1_ItemDataBound Item: {0}, Type: {1}, IsInEditMode: {2}",
                            e.Item,e.Item.ItemType,e.Item.IsInEditMode),ModuleTraceSwitch.Sw.Info);
                    GridEditFormItem item       =   (GridEditFormItem)e.Item;
                    if ( item.IsInEditMode )
                    {
                        if (ModuleTraceSwitch.Sw.TraceVerbose)
                            Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]UsersDownLoads RadGrid1_ItemDataBound DataItem: {0}",e.Item.DataItem.GetType()),ModuleTraceSwitch.Sw.Info);
                        // nothing to do on insert
                        if ( item.OwnerTableView.IsItemInserted )
                            return;
                        // fill combobox with users
                        MembershipUserCollection   tbUsers  =   null;
                        try
                        {
                            tbUsers                 =   Membership.GetAllUsers();
                            RadGrid1.DataSource     =   tbUsers;
                        }
                        catch (Exception ex)
                        {
                            if (ModuleTraceSwitch.Sw.TraceError)
                                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]UsersDownLoads RadGrid1_NeedDataSource ex: {0}.",ex),ModuleTraceSwitch.Sw.Info);
                            throw;
                        }
                        if (ModuleTraceSwitch.Sw.TraceVerbose)
                            Trace2.WriteLine("[V]UsersDownLoads RadGrid1_ItemDataBound END.",ModuleTraceSwitch.Sw.Info);
                    }
                }
            }
            else if ( e.Item.OwnerTableView.Name == "Products" )
            {
                if (e.Item is GridEditFormItem )
                {
                    if (ModuleTraceSwitch.Sw.TraceVerbose)
                        Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]UsersDownLoads RadGrid1_ItemDataBound Item: {0}, Type: {1}, IsInEditMode: {2}",
                            e.Item,e.Item.ItemType,e.Item.IsInEditMode),ModuleTraceSwitch.Sw.Info);
                    GridEditFormItem item       =   (GridEditFormItem)e.Item;
                    if ( item.IsInEditMode )
                    {
                        if (ModuleTraceSwitch.Sw.TraceVerbose)
                            Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]UsersDownLoads RadGrid1_ItemDataBound DataItem: {0}",e.Item.DataItem.GetType()),ModuleTraceSwitch.Sw.Info);
                        RadComboBox rdcbx                       =   null;
                        SqlDataReader dr                        =   null;
                        try
                        {  
                            ((RadDatePicker)item["StartDate"].Controls[0]).SelectedDate =   DateTime.Now;
                            ((RadDatePicker)item["EndDate"].Controls[0]).SelectedDate   =   DateTime.Now.AddMonths(WarrantyPeriod);
                            ((RadNumericTextBox)item["NbDownload"].Controls[0]).Value   =   0;
                            rdcbx                               =   (RadComboBox)item["ProductId"].Controls[0];
                            if (ModuleTraceSwitch.Sw.TraceVerbose)
                                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]UsersDownLoads RadGrid1_ItemDataBound rdcbx null: {0}",(rdcbx == null)),ModuleTraceSwitch.Sw.Info);
                            dr                                  =   dwnDB.GetAllProd2DownLoadForBuyers();
                            if (ModuleTraceSwitch.Sw.TraceVerbose)
                                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]UsersDownLoads RadGrid1_ItemDataBound GetAllProd2DownLoadForBuyers HasRows: {0}",dr.HasRows),ModuleTraceSwitch.Sw.Info);
                            rdcbx.Items.Clear();
                            while( dr.Read() )
                            {
                                Guid ProductId                  =   (Guid)dr["ProductId"];
                                if (ModuleTraceSwitch.Sw.TraceVerbose)
                                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]UsersDownLoads RadGrid1_ItemDataBound Adding ProductId: {0}",ProductId),ModuleTraceSwitch.Sw.Info);
                                ADNDynEProduct prod             =   new ProductsCRM().GetProduct(ProductId);
                                if (prod != null)
                                    rdcbx.Items.Add(new RadComboBoxItem(prod.name,ProductId.ToString()));
                            }
                            if (!item.OwnerTableView.IsItemInserted)
                            {
                                DbDataRecord rw                 =   item.DataItem as DbDataRecord;
                                Guid ProductId                  =   (Guid)rw["ProductId"];
                                RadComboBoxItem itm             =   rdcbx.Items.FindItemByValue(ProductId.ToString());
                                if ( itm != null )
                                    itm.Selected                =   true;
                                ((RadDatePicker)item["StartDate"].Controls[0]).SelectedDate =   (DateTime)rw["StartDate"];
                                ((RadDatePicker)item["EndDate"].Controls[0]).SelectedDate   =   (DateTime)rw["EndDate"];
                                ((RadNumericTextBox)item["NbDownload"].Controls[0]).Value   =   (int)rw["NbDownload"];
                            }
                        }
                        catch(Exception ex)
                        { 
                            if (ModuleTraceSwitch.Sw.TraceError)
                                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]UsersDownLoads RadGrid1_ItemDataBound ex: {0}",ex),ModuleTraceSwitch.Sw.Info);
                            throw;
                        }
                        finally
                        {
                            if ( dr != null )
                                dr.Close();
                        }
                        if (ModuleTraceSwitch.Sw.TraceVerbose)
                            Trace2.WriteLine("[V]UsersDownLoads RadGrid1_ItemDataBound END.",ModuleTraceSwitch.Sw.Info);
                    }
                }
                else if (e.Item is GridDataItem)
                {
                    GridDataItem item   =   e.Item as GridDataItem;
                    // retrieve current field title and  isMAin flag and set values
                    try
                    {  
                        DbDataRecord rw                 =   item.DataItem as DbDataRecord;
                        Guid ProductId                  =   (Guid)rw["ProductId"];
                        ADNDynEProduct prod             =   new ProductsCRM().GetProduct(ProductId);
                        if (prod != null)
                            item["ProductId"].Text      =   prod.name;
                    }
                    catch(Exception ex)
                    { 
                        if (ModuleTraceSwitch.Sw.TraceError)
                            Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]UsersDownLoads RadGrid1_ItemDataBound ex: {0}",ex),ModuleTraceSwitch.Sw.Info);
                        throw;
                    }
                }
            }
        }

        void RadGrid1_DeleteCommand(object source, GridCommandEventArgs e)
        {
			if (ModuleTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]UsersDownLoads RadGrid1_DeleteCommand BEG OwnerTableView.Name: {0}",e.Item.OwnerTableView.Name),ModuleTraceSwitch.Sw.Info);
            GridDataItem dataItem   =   (GridDataItem)e.Item;
            if (e.Item.OwnerTableView.Name == "Products")
            {
                int UserDownLoadId  =   (int)dataItem.GetDataKeyValue("UserDownLoadId");
                try
                {
                    dwnDB.DeleteUserDownLoad(UserDownLoadId);
                }
                catch (Exception ex)
                {
                    if (ModuleTraceSwitch.Sw.TraceError)
                        Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]UsersDownLoads RadGrid1_DeleteCommand Error deleting UserDownLoadId: {0}, ex: {1}", UserDownLoadId, ex), ModuleTraceSwitch.Sw.Info);
                    throw;
                }
                return;
            }
            else if (e.Item.OwnerTableView.Name == "Users")
            {
                Guid UserId  =   (Guid)dataItem.GetDataKeyValue("UserId");
                try
                {
                    dwnDB.DeleteUserDownLoadByUser(UserId);
                }
                catch (Exception ex)
                {
                    if (ModuleTraceSwitch.Sw.TraceError)
                        Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]UsersDownLoads RadGrid1_DeleteCommand Error deleting UserId: {0}, ex: {1}", UserId, ex), ModuleTraceSwitch.Sw.Info);
                    throw;
                }
            }

            if (ModuleTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine("[V]UsersDownLoads RadGrid1_DeleteCommand END.",ModuleTraceSwitch.Sw.Info);
        }

        void RadGrid1_UpdateCommand(object source, GridCommandEventArgs e)
        {
			if (ModuleTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]UsersDownLoads RadGrid1_UpdateCommand BEG OwnerTableView.Name: {0}",e.Item.OwnerTableView.Name),ModuleTraceSwitch.Sw.Info);
            GridEditableItem editedItem     =   e.Item as GridEditableItem;
            Hashtable newValues             =   new Hashtable();
            if (e.Item.OwnerTableView.Name == "Products")
            {
                e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem);
                if (ModuleTraceSwitch.Sw.TraceVerbose)
                {
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]UsersDownLoads RadGrid1_UpdateCommand newValues.Count: {0}", newValues.Count), ModuleTraceSwitch.Sw.Info);
                    foreach (DictionaryEntry ob in newValues)
                        Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]UsersDownLoads RadGrid1_UpdateCommand ob: {0}, key: {1}", ob.Value,ob.Key), ModuleTraceSwitch.Sw.Info);
                }
                try
                {
                    Guid ProductId              =   new Guid((string)newValues["ProductId"]);
                    DateTime StartDate          =   DateTime.Parse((string)newValues["StartDate"]);
                    DateTime EndDate            =   DateTime.Parse((string)newValues["EndDate"]);
                    if ( EndDate < StartDate )
                        EndDate                 =   StartDate.AddMonths(WarrantyPeriod);
                    int NbDownload              =   int.Parse((string)newValues["NbDownload"]);
                    int UserDownloadId          =   (int)editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["UserDownLoadId"];
                    Guid UserId                 =   (Guid)editedItem.OwnerTableView.ParentItem.OwnerTableView.DataKeyValues[editedItem.OwnerTableView.ParentItem.ItemIndex]["UserId"];
			        if (ModuleTraceSwitch.Sw.TraceVerbose)
                        Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]UsersDownLoads RadGrid1_UpdateCommand UserId: {0}",UserId),ModuleTraceSwitch.Sw.Info);
                    dwnDB.UpdateUserDownLoad(UserDownloadId,UserId,ProductId,StartDate,EndDate,NbDownload);
			        if (ModuleTraceSwitch.Sw.TraceVerbose)
                        Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]UsersDownLoads RadGrid1_UpdateCommand END UserDownloadId: {0}",UserDownloadId),ModuleTraceSwitch.Sw.Info);
                }
                catch (Exception ex)
                {
				    if (ModuleTraceSwitch.Sw.TraceError)
                        Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]UsersDownLoads RadGrid1_UpdateCommand ex: {0}",ex),ModuleTraceSwitch.Sw.Info);
                    throw;
                }
            }
        }

        private void RadGrid1_InsertCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
			if (ModuleTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]UsersDownLoads RadGrid1_InsertCommand BEG OwnerTableView.Name: {0}",e.Item.OwnerTableView.Name),ModuleTraceSwitch.Sw.Info);
            GridEditableItem editedItem         =   e.Item as GridEditableItem;
            if ( e.Item.OwnerTableView.Name == "Products" )
            {
                GridDataItem parentItem     =   (GridDataItem)(e.Item.OwnerTableView.ParentItem);
                Guid UserId                 =   (Guid)(parentItem.OwnerTableView.DataKeyValues[parentItem.ItemIndex][ "UserId"]);
			    if (ModuleTraceSwitch.Sw.TraceVerbose)
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]UsersDownLoads RadGrid1_InsertCommand UserId: {0}",UserId),ModuleTraceSwitch.Sw.Info);
                Hashtable newValues         =   new Hashtable();
                e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem);
                try
                {
                    Guid ProductId              =   new Guid((string)newValues["ProductId"]);
                    DateTime StartDate          =   DateTime.Parse((string)newValues["StartDate"]);
                    DateTime EndDate            =   DateTime.Parse((string)newValues["EndDate"]);
                    if ( EndDate < StartDate )
                        EndDate                 =   StartDate.AddMonths(WarrantyPeriod);
                    int NbDownload              =   int.Parse((string)newValues["NbDownload"]);
			        if (ModuleTraceSwitch.Sw.TraceVerbose)
                        Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]UsersDownLoads RadGrid1_UpdateCommand ProductId: {0}",ProductId),ModuleTraceSwitch.Sw.Info);
                    dwnDB.AddUserDownLoad(UserId,ProductId,StartDate,EndDate,NbDownload);
			        if (ModuleTraceSwitch.Sw.TraceVerbose)
                        Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]UsersDownLoads RadGrid1_InsertCommand END UserId: {0}",UserId),ModuleTraceSwitch.Sw.Info);
                }
                catch (SqlException sqex)
                {
				    if (ModuleTraceSwitch.Sw.TraceError)
                        Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]UsersDownLoads RadGrid1_InsertCommand sqex: {0}",sqex),ModuleTraceSwitch.Sw.Info);
                    SetMessage("Existing product");
                    e.Canceled          =   true;
                    return;
                }
                catch (Exception ex)
                {
				    if (ModuleTraceSwitch.Sw.TraceError)
                        Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]UsersDownLoads RadGrid1_InsertCommand ex: {0}",ex),ModuleTraceSwitch.Sw.Info);
                    e.Canceled  =   true;
                    throw;
                }
                SetMessage("Product is updated!");
            }
        }
        
        private void DisplayMessage(string text)
        {
            RadGrid1.Controls.Add(new LiteralControl(string.Format("<span style='color:red'>{0}</span>", text)));
        }

        private void SetMessage(string message)
        {
            gridMessage = message;
        }
        
        private string gridMessage = null;
        protected void RadGrid1_DataBound(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(gridMessage))
            {
                DisplayMessage(gridMessage);
            }
        }

        void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
        {
			if (ModuleTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]UsersDownLoads RadGrid1_ItemCommand BEG e.Item.OwnerTableView.Name: {0}, CommandName: {1}",
                    ( e.Item != null) ? e.Item.OwnerTableView.Name:"null",e.CommandName),ModuleTraceSwitch.Sw.Info);
            ///if (e.Item.OwnerTableView.Name == "Users" && e.CommandName == RadGrid.InitInsertCommandName )
            if ( ((e.Item == null) || (e.Item.OwnerTableView.Name == "Users"))&& (e.CommandName == RadGrid.InitInsertCommandName ))
            {
			    if (ModuleTraceSwitch.Sw.TraceVerbose)
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]UsersDownLoads RadGrid1_ItemCommand redirecturl: {0}",AddButton.HRef),ModuleTraceSwitch.Sw.Info);
                // Simulate the Add button
                //Response.Redirect(AddButton.HRef,true);
                ADNPage.Redirect(AddButton.HRef,true);
            }
        }

        protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
			if (ModuleTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]UsersDownLoads RadGrid1_NeedDataSource BEG e.IsFromDetailTable: {0}",
                    e.IsFromDetailTable),ModuleTraceSwitch.Sw.Info);
            if (!e.IsFromDetailTable)
            {
                RadGrid1.DataSource         =   dwnDB.GetAllUserDownLoad();
            }
        }

        protected void RadGrid1_DetailTableDataBind(object source, Telerik.Web.UI.GridDetailTableDataBindEventArgs e)
        {
			if (ModuleTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]UsersDownLoads RadGrid1_DetailTableDataBind BEG e.DetailTableView.Name: {0}",
                    e.DetailTableView.Name),ModuleTraceSwitch.Sw.Info);
            GridDataItem dataItem                       =   (GridDataItem)e.DetailTableView.ParentItem;
            switch (e.DetailTableView.Name)
            {
                case "Products":
                    {
                        Guid UserId                     =   (Guid)dataItem.GetDataKeyValue("UserId");
			            if (ModuleTraceSwitch.Sw.TraceVerbose)
                            Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]UsersDownLoads RadGrid1_DetailTableDataBind UserId: {0}",
                                UserId),ModuleTraceSwitch.Sw.Info);
                        e.DetailTableView.DataSource    =   dwnDB.GetUserDownLoadByUserId(UserId);
                        break;
                    }

            }
            if (ModuleTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine("[V]UsersDownLoads RadGrid1_DetailTableDataBind END.",ModuleTraceSwitch.Sw.Info);
        }

        #region Std overrides 

        public override void Page_PreInit(object sender, EventArgs e)
        {
            AddText                     =   "ADD";
            AddUrl                      =   Config.ModulesPath + "/DownLoad/AddUsersDownloads.aspx";
            base.Page_PreInit(sender, e);
            string strcult              =   Helpers.LocalizeHelper.GetCurrentNeutralCultureName();
        }

		override protected void OnInit(EventArgs e)
		{
			InitializeComponent();
			base.OnInit(e);
		}

		private void InitializeComponent()
		{
            Load                            +=  new EventHandler(Page_Load);
            RadGrid1.NeedDataSource         +=  new GridNeedDataSourceEventHandler(RadGrid1_NeedDataSource);
            RadGrid1.DetailTableDataBind    +=  new GridDetailTableDataBindEventHandler(RadGrid1_DetailTableDataBind);
            RadGrid1.DeleteCommand          +=  new GridCommandEventHandler(RadGrid1_DeleteCommand);
            RadGrid1.InsertCommand          +=  new GridCommandEventHandler(RadGrid1_InsertCommand);
            RadGrid1.UpdateCommand          +=  new GridCommandEventHandler(RadGrid1_UpdateCommand);
            RadGrid1.ItemDataBound          +=  new GridItemEventHandler(RadGrid1_ItemDataBound);
            RadGrid1.ItemCommand            +=  new GridCommandEventHandler(RadGrid1_ItemCommand);
            RadGrid1.DataBound              +=  new EventHandler(RadGrid1_DataBound);
 		}

        #endregion
    }
}